﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using YasharEl.Infrastructure.ApplicationModel.WebService.Bootstrap;
using YasharEl.Infrastructure.ApplicationModel.Bootstrap;
using YasharEl.Infrastructure.ApplicationModel.Bootstrap.Extensions;
using YasharEl.Infrastructure.ExceptionHandling;

namespace YasharEl.Infrastructure.TaskEngine.BackendApplication
{
    public class Global : System.Web.HttpApplication
    {
        private static WebServiceApplicationUnityBootstrapper bootstrapper = null;

        protected void Application_Start(object sender, EventArgs e)
        {
            bootstrapper = new WebServiceApplicationUnityBootstrapper();
            ComplexBootstrappingStrategy bootsrapperStrategy = new ComplexBootstrappingStrategy();

            bootstrapper.Initialize(bootsrapperStrategy);

            // Add exception handling cross cutting infrastructure extension...
            ExceptionHandlingBootstrapperExtension exceptionHandlingBootstrapperExtension = new ExceptionHandlingBootstrapperExtension();
            bootstrapper.AddExtension(exceptionHandlingBootstrapperExtension);

            // Add entities services(validation,unit of factory,prototype properties,specifications and etc) extension
            bootstrapper.AddExtension(new EntitiesModelServicesConfigurationBootstrapperExtension());

            // Add application modularity extension...
            bootstrapper.AddExtension(new ApplicationModularityConfigurationBootstrapperExtension());

            bootstrapper.Run();
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();
            Exception exceptionToThrow;
            if (exception != null)
            {
                if (exception is HttpUnhandledException)
                    exception = exception.InnerException;

                if (bootstrapper.IsBootstrapperSuccess)
                {
                    IExceptionManager exceptionManager = null;
                    try
                    {
                        exceptionManager = bootstrapper.Container.DependencyResolver.Resolve<IExceptionManager>();
                    }
                    finally
                    {
                        if (exceptionManager != null)
                        {
                            if (exceptionManager.HandleException(exception, "Abnormal Termination Exception Handling Policy", out exceptionToThrow))
                            {
                                if (exceptionToThrow != null)
                                {
                                    throw exceptionToThrow;
                                }
                                else
                                {
                                    throw exception;
                                }
                            }
                            else
                            {
                                Server.ClearError();
                            }
                        }
                    }
                }
            }
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
            if (bootstrapper != null)
                bootstrapper.Shutdown();
        }
    }
}